﻿using AutoMapper;
using BaseData.API.Read.Applications.Commands;
using BaseData.API.Read.Dtos;
using BaseData.Domain;
using BaseData.ErrorCode;
using BaseData.Infrastructure.Interfaces;
using MD5Hash;
using MediatR;
using System.Collections.Generic;

namespace BaseData.API.Read.Applications.CommandHandlers
{
    public class UserQueryCommandHandler : IRequestHandler<UserQueryCommand, ApiResult<ApiPaging<List<UsersDTO>>>>
    {
        private readonly IBaseRepository<User> userRep;
        private readonly IBaseRepository<Role> roleRep;
        private readonly IBaseRepository<UserRoleRelation> userRoleRep;
        private readonly IMapper mapper;

        public UserQueryCommandHandler(IBaseRepository<User> userRep, IBaseRepository<Role> roleRep, IBaseRepository<UserRoleRelation> userRoleRep,
            IMapper mapper)
        {
            this.userRep = userRep;
            this.roleRep = roleRep;
            this.userRoleRep = userRoleRep;
            this.mapper = mapper;
        }
        public Task<ApiResult<ApiPaging<List<UsersDTO>>>> Handle(UserQueryCommand request, CancellationToken cancellationToken)
        {
            ApiResult<ApiPaging<List<UsersDTO>>> result = new ApiResult<ApiPaging<List<UsersDTO>>>();
            result.Code = ApiEnum.查询成功;
            result.Message=ApiEnum.查询成功.ToString();

            var query = userRep.GetAll();

            //var list = from a in userRep.GetAll()
            //           join b in userRoleRep.GetAll()
            //           on a.UserId equals b.UserId
            //           join c in roleRep.GetAll()
            //           on b.RoleId equals c.RoleId
            //           select new UserDTO
            //           {
            //               UserId = a.UserId,
            //               UserName = a.UserName,
            //               UserNickname = a.UserNickname,
            //               RoleId = b.RoleId,
            //               RoleName = c.RoleName,
            //               CreateTime=a.CreateTime,
            //               Status=a.Status,
            //           };
            if (!string.IsNullOrEmpty(request.UserNickname))
            {
                query = query.Where(x => x.UserNickname.Contains(request.UserNickname));
            }
            //
            //分页
            int TotalCount= query.Count();
            int PageCount=(int)Math.Ceiling((double)TotalCount/request.PageSize);

            var list = query.OrderBy(x => x.UserId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();

            var data=mapper.Map<List<UsersDTO>>(list);
            foreach (var item in data)
            {
                var roleIds = userRoleRep.GetAll().Where(x => x.UserId == item.UserId).Select(x => x.RoleId).ToList();
                var roleNames=roleRep.GetAll().Where(x=>roleIds.Contains(x.RoleId)).Select(x=>x.RoleName).ToList();
                item.RoleId=roleIds;
                item.RoleName = roleNames;
            }

            ApiPaging<List<UsersDTO>> paging = new ApiPaging<List<UsersDTO>>();
            paging.TotalCount=TotalCount;
            paging.PageCount=PageCount;
            paging.Data = data;

           result.Data=paging;

            return Task.FromResult(result);
        }
    }
}
